-
Notifications
You must be signed in to change notification settings - Fork 1.4k
MONGOID-5882 Isolation state #6009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Does |
It shouldn't. That's what most of the tests in this PR are designed to ensure: that nested fibers inherit state from their parent fiber. The entire point of this PR is to make sure Mongoid can be compatible with fiber-based libraries like Falcon. |
Cool. 2 more:
|
Awesome, thanks @jamis! Only one comment from me, does it make sense to default the value of P.S. Falcon also assumes this is the standard entry point to make such decisions: https://github.com/socketry/falcon/blob/main/lib/falcon/railtie.rb |
@travisbell -- great suggestion. I'll look into it. It's a bit complicated by the fact that we still support Rails 6.x, and the |
It's worth considering. Again, "aside from not being on the latest Ruby" is the sticking point. As long as we support Ruby prior to 3.2, we really can't make |
@travisbell I've updated the PR so that Mongoid will default the isolation state to whatever it is set to in Rails; it had to take several edge cases into account, but it works now. Would you be able to try this PR with your app, to see if it mitigates some of the problems you were seeing with Mongoid+Falcon? Otherwise, I'll see if I can unpack Falcon enough to set something up locally, myself. |
@jamis I just left for vacation but when I get back (end of July), I’ll give it a spin and report back. 👍🏼 |
@jamis I've been running this branch in prod for the past day or so and everything is looking great. I haven't seen the previous issue with mis-matched queries so I believe this is looking good. With these changes I think we can officially say Falcon is supported! 💪🏼 |
@travisbell That's wonderful! Thanks for giving it a spin and letting me know. That gives me confidence to move forward with this. |
Hey @jamis, turns out using I currently have this commit running in production, which seems to properly solve it, simply using I think there's some opportunity to optimize these lookups a bit, for example, I don't think we need to continuously evaluate which isolation scope to use on every single Anywho, let me know what you think about a change like this. |
@travisbell, thanks for the follow up. One of the problems with fibers in general, here, is that it is entirely possible to run fibers within the context of other fibers, which is not a situation that can occur with threads. Ruby's fiber-local storage allows fiber state to be inherited by these child fibers, which is (I believe) a feature we need. Otherwise, it is possible for Mongoid to "lose track of itself" in certain query configurations, when it spawns fibers itself to process callbacks on embedded children. (Those spawned fibers need to be able to see the Mongoid state that was created by the spawning fiber.) I know concurrency issues are difficult to nail down, but is there any way you could provide a way for me to reproduce the issues you're seeing? I tried several things when I was developing this PR, but everything I tried was probably far too naive to actually tickle the bug. |
Knowing how it's failing in our environment might make it a bit easier for me to create a reproduction. Let me take a stab at it... |
Some applications will use threads for concurrency. Others will use fibers. Prior to this PR, Mongoid worked fine with threads, but it's internal state could get into odd states when run under fibers.
This PR allows applications to indicate which isolation level they wish to use, and Mongoid's state will be isolated to that scope.
When using the
:fiber
isolation level, Mongoid's internal state will be inherited from any parent fiber. If you want to make sure a fiber begins with a clean slate, you can wipe the isolation state withMongoid::Threaded.reset!
.